package com.ppch.wuwamanus.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.ppch.wuwamanus.model.dto.draw_card_analysis.CardAnalysisRequest;
import com.ppch.wuwamanus.tools.CardDrawingAnalysisTool;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.Map;

/**
 * Package:WuWaManus
 * ClassName:HealthController
 *
 * @Author 泡泡茶壶
 * @Create 2025/8/5 20:58
 * @Version 1.0
 * Description:
 * 健康检测
 */
@RestController
@RequestMapping("/health")
public class HealthController {
    /**
     * 健康检查接口
     *
     * @return success
     */
    @GetMapping
    public String testHealth() {
        return "success";
    }

    /**
     * 获取用户的抽卡结果
     * 1.接口请求地址：https://gmserver-api.aki-game2.com/gacha/record/query
     *
     * @return String
     */
    @PostMapping("/analysis")
    public String testCardDrawingAnalysis(@RequestBody CardAnalysisRequest cardAnalysisRequest) {

        String cardPoolId = cardAnalysisRequest.getCardPoolId();
        Integer cardPoolType = cardAnalysisRequest.getCardPoolType();
        String languageCode = cardAnalysisRequest.getLanguageCode();
        String playerId = cardAnalysisRequest.getPlayerId();
        String recordId = cardAnalysisRequest.getRecordId();
        String serverId = cardAnalysisRequest.getServerId();

        // 接口地址
        String cardDrawingResultUrl = "https://gmserver-api.aki-game2.com/gacha/record/query";

        // 构建JSON请求体
        JSONObject json = new JSONObject();
        json.set("cardPoolId", cardPoolId)
                .set("cardPoolType", cardPoolType)
                .set("languageCode", languageCode)
                .set("playerId", playerId)
                .set("recordId", recordId)
                .set("serverId", serverId);//不同玩家有不同的serverId

        try {
            // 发送POST请求（关键修改：取消header设置的注释）
            HttpResponse response = HttpRequest.post(cardDrawingResultUrl)
                    .header("Content-Type", "application/json; charset=UTF-8")
                    .body(json.toString())
                    .timeout(30000)
                    .execute();

            // 获取响应结果
            if (response.isOk()) {
                String body = response.body();
                System.out.println("响应成功: " + body);
                return body;
            } else {
                System.out.println("请求失败: HTTP状态码 " + response.getStatus());
                System.out.println("错误信息: " + response.body());
                return "请求失败: HTTP状态码 " + response.getStatus();
            }
        } catch (Exception e) {
            System.err.println("请求异常: " + e.getMessage());
            e.printStackTrace();
            return "请求异常: " + e.getMessage();
        }
    }


    /**
     * 1.获取《鸣潮》抽卡分析结果页面 URL 中的查询参数
     * 2.将查询参数作为请求体，发送请求获取用户的抽卡结果列表
     *
     * @param url 《鸣潮》抽卡分析结果页面URL
     */
    @GetMapping("/card_drawing_result")
    public String getCardDrawingResult(String url) {
        //1.获取《鸣潮》抽卡分析结果页面 URL 中的查询参数
        Map<String, String> params = null;
        try {
            params = new CardDrawingAnalysisTool().extractQueryParameters(url);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        System.out.println("提取参数:");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        }

        //2.将查询参数作为请求体，发送请求获取用户的抽卡结果列表
        // 接口地址
        String cardDrawingResultUrl = "https://gmserver-api.aki-game2.com/gacha/record/query";
        // 构建JSON请求体
        JSONObject json = new JSONObject();
        json.set("cardPoolId", params.get("gacha_id"))
                .set("cardPoolType", params.get("gacha_type"))
                .set("languageCode", params.get("lang"))
                .set("playerId", params.get("player_id"))
                .set("recordId", params.get("record_id"))
                .set("serverId", params.get("svr_id"));//不同玩家有不同的serverId

        try {
            // 发送POST请求（关键修改：取消header设置的注释）
            HttpResponse response = HttpRequest.post(cardDrawingResultUrl)
                    .header("Content-Type", "application/json; charset=UTF-8")
                    .body(json.toString())
                    .timeout(30000)
                    .execute();

            // 获取响应结果
            if (response.isOk()) {
                String body = response.body();
                System.out.println("响应成功: " + body);
                return body;
            } else {
                System.out.println("请求失败: HTTP状态码 " + response.getStatus());
                System.out.println("错误信息: " + response.body());
                return "请求失败: HTTP状态码 " + response.getStatus();
            }
        } catch (Exception e) {
            System.err.println("请求异常: " + e.getMessage());
            e.printStackTrace();
            return "请求异常: " + e.getMessage();
        }

    }


}
